import billDao from "../Dao/bill.js";
import companyDao from "../Dao/company.js";
import companyUserDao from "../Dao/company_user.js";
import {CompanyRole} from "../roleTypes/CompanyRole.js";
import depotDao from "../Dao/depot.js";
import SafeCalc from "../utils/bignumberjs.js";
import userDao from "../Dao/user.js";
import categoryDao from "../Dao/category.js";

/**
 * 检查用户id 不存在则报错
 * @param id
 */
export const queryCheckId = async (id) => {
  if (!id) {
    throw new Error('用户id错误');
  }
  let user = await userDao.findUserLock(id);
  if (!user) {
    throw new Error('用户不存在');
  }
};
/**
 * 检查账单类型是否存在 不存在则报错
 * @param category
 * @returns {Promise<void>}
 */
export const checkBillCategory = async (category) => {
  let billCategory = await categoryDao.getCategoryData(category);
  // console.log(billCategory, '检查账单类型是否存在');
  if(!billCategory) {
    throw new Error('账单分类不存在');
  }
};
/**
 *  检查用户是否存在
 * @param userInfo
 */
export const checkUserExist = (userInfo) => {
  if (!userInfo) {
    throw new Error('用户不存在');
  }
};
/**
 * 检查用户账单类别名称是否已经存在
 * @param name
 * @param userId
 * @returns {Promise<void>}
 */
export const checkPersonCategoryNameExist = async (name, userId) => {
  let category = await categoryDao.getPersonCategoryDataByName(name, userId);
  if (category) {
    throw new Error('分类名称已存在');
  }
};
/**
 * 检查用户是否是这个账单分类创建者
 * @param creator
 * @param categoryId
 * @returns {Promise<void>}
 */
export const checkPersonCategoryCreator = async (creator, categoryId) => {
  let category = await categoryDao.getPersonCategoryByUserId(categoryId, creator);
  if (!category) {
    throw new Error('无权限修改');
  }
};
/**
 * 检查是否是个人账单创建者
 * @param creator
 * @param billId
 * @returns {Promise<void>}
 */
export const checkPersonBillCreator = async (creator, billId) => {
  let creatorTemp = await billDao.getCreator(billId);
  if (!creatorTemp || creator.toString() !== creatorTemp.creator_id.toString()) {
    throw new Error("无权限修改");
  }
};
/**
 * 检查公司名称是否已存在
 * @param companyName
 * @returns {Promise<void>}
 */
export const checkCompanyNameExist = async (companyName) => {
  if(!companyName) {
    throw new Error("公司名称为空");
  }
  let company = await companyDao.isCompanyNameExist(companyName);
  if(company) {
    throw new Error("该公司名称已存在");
  }
};
/**
 * 创建或者更新公司员工角色
 * @param company_id
 * @param user_id
 * @param role
 * @returns {Promise<void>}
 */
export const updateUserCompanyRole = async (company_id, user_id, role) => {
  // 获取员工在公司角色
  const user = await companyUserDao.getRoleByUserId(company_id, user_id);
  // 判断是否是公司员工
  if(!user) {
    // 插入新的公司员工
    await companyUserDao.createCompanyUser(company_id, role, user_id);
  } else {
    // 判断公司角色是否是小于这个任命的角色
    if(user.role === CompanyRole.CREATOR) {
      throw new Error("创建者不能变为其他职位");
    }
    // 更新公司员工角色
    await companyUserDao.updateCompanyUserRole(company_id, user_id, role);
  }
};
/**
 *  判断用户是否是公司创建者
 * @param company_id
 * @param user_id
 * @returns {Promise<void>}
 */
export const checkUserCompanyCreator = async (company_id, user_id) => {
  // 判断发起请求的用户是否是公司创建者
  const creator = await companyUserDao.getRoleByUserId(company_id, user_id);
  if(creator.role !== CompanyRole.CREATOR || !creator) {
    throw new Error('不是公司创建者');
  }
};
/**
 *  判断用户是否是公司该角色
 * @param company_id
 * @param user_id
 * @param role
 * @returns {Promise<void>}
 */
export const checkUserCompanyRole = async (company_id, user_id, role) => {
  const user = await companyUserDao.getRoleByUserId(company_id, user_id);
  if(user.role !== role || !user) {
    throw new Error('公司角色错误');
  }
};
/**
 * 判断用户是否是公司属于这些角色
 * @param company_id
 * @param user_id
 * @param roles
 * @returns {Promise<void>}
 */
export const checkUserInCompanyRole = async (company_id, user_id, roles) => {
  const user = await companyUserDao.getRoleByUserId(company_id, user_id);
  if(!user || !roles.includes(user.role)) {
    throw new Error('公司角色错误');
  }
};
/**
 * 判断用户是否是公司员工
 * @param company_id
 * @param user_id
 * @returns {Promise<void>}
 */
export const checkUserInCompany = async (company_id, user_id) => {
  const user = await companyUserDao.getRoleByUserId(company_id, user_id);
  if(!user) {
    throw new Error('用户不在公司');
  }
};
/**
 *
 * @param companyId
 * @param billId
 * @returns {Promise<*>}
 */
export const checkBillInCompany = async (companyId, billId) => {
  let bill = await billDao.getBillCompanyId(billId);
  if(!bill || bill.company_id.toString() !== companyId.toString()) {
    throw new Error('单据不属于该公司');
  }
  return bill;
};
/**
 * 判断公司是否存在
 * @param company_id
 * @returns {Promise<void>}
 */
export const checkCompanyExist = async (company_id) => {
  const company = await companyDao.getCompany(company_id);
  if (!company) {
    throw new Error('公司不存在');
  }
};
/**
 * 判断公司该账单类别名称是否存在
 * @param name
 * @param company_id
 * @returns {Promise<void>}
 */
export const checkCompanyCategoryNameExist = async (name, company_id) => {
  let categoryExist = await categoryDao.getCompanyCategoryDataByName(name, company_id);
  if(categoryExist) {
    throw new Error('品类已存在');
  }
};
/**
 * 检查账单是需要 category, remark, amount, isComplete
 * @param newValue
 * @param oldValue
 */
export const checkBillNeedUpdate = (newValue, oldValue) => {
  console.log(oldValue, newValue);
  let res = newValue.amount === oldValue.amount && newValue.category === oldValue.category && newValue.remark === oldValue.remark && newValue.isComplete === oldValue.isComplete;
  if(res) {
    throw new Error('账单不需要更新');
  }
};
/**
 * 检查仓库品类是否已存在 存在则报错
 * @param name
 * @param company_id
 * @returns {Promise<void>}
 */
export const checkDepotGoodsExist = async (name, company_id) => {
  let goodsExist = await depotDao.isExistByName(name, company_id);
  if(goodsExist) {
    throw new Error('品类已存在');
  }
};
/**
 * 检查仓库品类是否在该公司中 不在则报错
 * @param goodsId
 * @param company_id
 * @returns {Promise<void>}
 */
export const checkDepotGoodsInCompany = async (goodsId, company_id) => {
  let res = await depotDao.isBelongToCompany(goodsId, company_id);
  if(!res) {
    throw new Error('品类不在该公司中');
  }
};
/**
 * 添加仓库物品数量
 * @param goodsId
 * @param amount
 * @returns {Promise<void>}
 */
export const addDepotGoods = async (goodsId, amount, t) => {
  // 查询品类原来的数量
  let goods = await depotDao.getDepotDetail(goodsId, t);
  // 增加数量
  let newAmount = SafeCalc.add(goods.amount, amount);
  // 写入数据库
  await depotDao.updateDepotAmount(goodsId, newAmount, t);
};
/**
 * 减少仓库物品数量 不够就报错
 * @param goodsId
 * @param amount
 * @returns {Promise<void>}
 */
export const subDepotGoods = async (goodsId, amount, t) => {
  // 查询品类原来的数量
  let goods = await depotDao.getDepotDetail(goodsId, t);
  // 比较数量是否足够
  if(Number(goods.amount) < Number(amount)) {
    throw new Error('库存不足');
  }
  // 增加数量
  let newAmount = SafeCalc.sub(goods.amount, amount);
  // 写入数据库
  await depotDao.updateDepotAmount(goodsId, newAmount, t);
};
